<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="100px"
      v-show="showSearch"
    >
      <el-form-item label="客户名称" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信昵称" prop="vxName">
        <el-input
          v-model="queryParams.vxName"
          placeholder="请输入微信昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户联系方式" prop="memberPhone">
        <el-input
          v-model="queryParams.memberPhone"
          placeholder="请输入客户联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠券名称" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入优惠券名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠券状态" prop="memberCouponStatus">
        <el-select
          v-model="queryParams.memberCouponStatus"
          placeholder="客户优惠券状态"
          clearable
          size="small"
        >
          <el-option
            v-for="item in dict.type.coupon_memberCouponStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优惠券来源" prop="sourceType">
        <el-select
          v-model="queryParams.sourceType"
          placeholder="优惠券来源"
          clearable
          size="small"
        >
          <el-option
            v-for="item in dict.type.coupon_sourceType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入优惠券业务类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          v-hasPermi="['coupon:member:query']"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['coupon:member:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="memberList">
      <!-- <el-table-column
        type="selection"
        width="55"
        align="center"
      /> -->
      <el-table-column
        min-width="160"
        label="客户名称"
        align="center"
        prop="memberName"
        show-overflow-tooltip
      />
      <el-table-column
        min-width="160"
        label="微信头像"
        align="center"
        prop="vxUrl"
      >
        <template slot-scope="scope">
          <el-image
            style="width: 48px; height: 48px"
            :src="scope.row.vxUrl"
            fit="fill"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="微信名称"
        align="center"
        prop="vxName"
      />
      <el-table-column
        min-width="120"
        label="客户联系方式"
        align="center"
        prop="memberPhone"
      />
      <el-table-column
        min-width="180"
        label="客户类型"
        align="center"
        prop="memberType"
      >
        <template slot-scope="scope">
          {{ scope.row.memberType | typeFilter(dict.type.coupon_memberType) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="优惠券名称"
        align="center"
        prop="couponName"
        sortable
      />
      <el-table-column
        min-width="160"
        label="客户优惠券状态"
        align="center"
        prop="memberCouponStatus"
        sortable
      >
        <template slot-scope="scope">
          {{
            scope.row.memberCouponStatus
              | typeFilter(dict.type.coupon_memberCouponStatus)
          }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="优惠券来源"
        align="center"
        prop="sourceType"
        sortable
      >
        <template slot-scope="scope">
          {{ scope.row.sourceType | typeFilter(dict.type.coupon_sourceType) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="优惠券领取时间"
        align="center"
        prop="distributedTime"
        sortable
      />
      <el-table-column
        min-width="160"
        label="优惠券有效时间"
        align="center"
        prop="validTime"
      />
      <el-table-column
        min-width="160"
        label="业务类型"
        align="center"
        prop="businessName"
        sortable
      />
      <el-table-column
        min-width="160"
        label="使用类型名称"
        align="center"
        prop="useTypeName"
      />
      <el-table-column
        min-width="160"
        label="创建人"
        align="center"
        prop="userName"
        sortable
      />
      <el-table-column
        min-width="160"
        label="手机型号"
        align="center"
        prop="phoneModel"
        sortable
      />
      <el-table-column
        min-width="160"
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handDetail(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getMemberList, exportMember } from '@/api/coupon/member'

export default {
  name: 'CouponMember',
  dicts: [
    'coupon_memberCouponStatus',
    'coupon_sourceType',
    'coupon_memberType',
  ],
  components: {},
  data() {
    return {
      showSearch: true,
      // 表格遮罩
      loading: false,
      // 表格数据
      memberList: [],
      total: 1,
      // 导出遮罩层
      exportLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberName: null,
        vxName: null,
        memberPhone: null,
        couponName: null,
        memberCouponStatus: null,
        sourceType: null,
        businessName: null,
      },
    }
  },
  filters: {
    typeFilter(val, type) {
      if (val === null) return null
      let data = type.filter((item) => {
        return val == item.value
      })
      if (data.length) {
        return data[0].label
      } else {
        return val
      }
    },
  },
  mounted() {
    this.getList()
  },
  methods: {
    /** 查询租户管理列表 */
    async getList() {
      this.loading = true
      await getMemberList(this.queryParams).then((response) => {
        this.memberList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 详情按钮操作 */
    handDetail(row) {
      this.$router.push({ path: '/coupon/member/detail/' + row.memberId })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = {
        memberName: this.queryParams.memberName,
        vxName: this.queryParams.vxName,
        memberPhone: this.queryParams.memberPhone,
        couponName: this.queryParams.couponName,
        memberCouponStatus: this.queryParams.memberCouponStatus,
        sourceType: this.queryParams.sourceType,
        businessName: this.queryParams.businessName,
      }
      this.$modal
        .confirm('是否确认导出所有客户优惠券数据项？')
        .then(() => {
          this.exportLoading = true
          exportMember(queryParams)
            .then((res) => {
              this.$download.name(res.msg)
              this.exportLoading = false
            })
            .catch((err) => {
              this.exportLoading = false
              console.log(err)
            })
        })
        .catch(() => {})
    },
  },
}
</script>
